package com.feifan.oa.service.impl;

import com.feifan.common.config.Global;
import com.feifan.common.core.text.Convert;
import com.feifan.common.utils.file.FileUploadUtils;
import com.feifan.oa.domain.Leave;
import com.feifan.oa.mapper.LeaveMapper;
import com.feifan.oa.service.ILeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 请假Service业务层处理
 * @author 非凡
 * @date 2020-07-04
 */
@Service
public class
LeaveServiceImpl implements ILeaveService {
    @Autowired
    private LeaveMapper leaveMapper;

    /**
     * 查询请假
     * @param id 请假ID
     * @return 请假
     */
    @Override
    public Leave selectLeaveById(Long id) {
        return leaveMapper.selectLeaveById(id);
    }

    @Override
    public List<Leave> selectLeaveListByUId(Long uId) {
        return leaveMapper.selectLeaveListByUId(uId);
    }

    /**
     * 查询请假列表
     * @param leave 请假
     * @return 请假
     */
    @Override
    public List<Leave> selectLeaveList(Leave leave) {
        return leaveMapper.selectLeaveList(leave);
    }

    /**
     * 新增请假
     * @param leave 请假
     * @return 结果
     */
    @Override
    public int insertLeave(Leave leave, MultipartFile multipartFile) throws IOException {
        //没有上传请假证明
        if (multipartFile.isEmpty()) {
            return leaveMapper.insertLeave(leave);
        }

        //上传请假证明
        String avatar = FileUploadUtils.upload(Global.getLeaveCertificatePath(), multipartFile);
        leave.setPicture(avatar);

        return leaveMapper.insertLeave(leave);

    }

    /**
     * 修改请假
     * @param leave 请假
     * @return 结果
     */
    @Override
    public int updateLeave(Leave leave) {
        return leaveMapper.updateLeave(leave);
    }

    /**
     * 删除请假对象
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteLeaveByIds(String ids) {
        return leaveMapper.deleteLeaveByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除请假信息
     * @param id 请假ID
     * @return 结果
     */
    @Override
    public int deleteLeaveById(Long id) {
        return leaveMapper.deleteLeaveById(id);
    }
}
